Functional Programming in PHP by Simon Holywell
Author:Simon Holywell [Simon Holywell]
Language: eng
Format: epub
Tags: Core Programming
Publisher: php[architect]
Published: 2016-11-02T04:00:00+00:00
At the other end of the equation we have tail that will return a list with all but the first value in the array contained in it.
function tail(array $arr) { return array_slice($arr, 1); } tail([1,2,3,4,5]); // [2,3,4,5]
These two functions can be used together to work through a list using recursion.
function print_items(array $arr) { echo head($arr) . '-'; if(tail($arr)) print_items(tail($arr)); } print_items([1,2,3,4,5]); // 1-2-3-4-5-
6.2 Flattening lists
Lists of lists can be very helpful when dealing with complex datasets or when transforming an array via array_map() where it would return an array from the applied function. In some instances though, you have a list of lists that really should just be one list with all values at the top level. The problem could look something like:
$arr = [1, 2, 3, 4, 5]; $divisor = 10.5; $arr2 = array_map(function($x) use ($divisor) { return [$x, $x / $divisor, $x % $divisor]; }, $arr); // [ // [1, 0.095238095238095233, 1], // [2, 0.19047619047619047, 2], // [3, 0.2857142857142857, 3], // [4, 0.38095238095238093, 4], // [5, 0.47619047619047616, 5] // ]
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27094)
Hello! Python by Anthony Briggs(25942)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25285)
Kotlin in Action by Dmitry Jemerov(24393)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23311)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21943)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20847)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17072)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16832)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12581)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10650)
Hit Refresh by Satya Nadella(9236)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8445)
Robo-Advisor with Python by Aki Ranin(8387)